package day1130book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import day1130.JDBCUtil;

public class RecordDao {
   //增加记录
	public static void insert(RecordModel rm) {
		String sql = "insert into record(id,name,time) values(?,?,?)";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, rm.getId());
			ps.setString(2, rm.getName());
			ps.setString(3, rm.getTime());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
	}
	
	//根据id查询记录
	public static ArrayList<RecordModel> select(RecordModel rm) {
		String sql = "select * from record where id = ?";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet result = null;
		ArrayList<RecordModel> list = new ArrayList<>();
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, rm.getId());
			result = ps.executeQuery();
			while(result.next()) {
				RecordModel r = new RecordModel();
				r.setId(result.getInt("id"));
				r.setName(result.getString("name"));
				r.setTime(result.getString("time"));
				list.add(r);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
}
